package it.blueocean.acanto.taglibrary.jsp.taglib.tags.dataset;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractComponentStateBean;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.commands.LinkTag;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.datatable.DataTableTag;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils.HtmlPrintHelper;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class DatasetNavigationTag extends LinkTag implements ConditionalTag
{
	private static final long serialVersionUID = -8654045875154439119L;

	private final static Logger logger_ = LoggerFactory.getLogger(DatasetNavigationTag.class);

	protected DataSet dataset_;

	protected String datasetName_;

	protected boolean hideWhenDisabled_;

	private String datasetNameExpr_;

	protected String hideWhenDisabledExpr_;

	private boolean enabled_;
	
	protected void evaluateExpressions() throws JspException
	{
		super.evaluateExpressions();
		DataTableTag localDataTableTag = (DataTableTag) findAncestorWithClass(this, DataTableTag.class);
		if(localDataTableTag != null)
		{
			this.dataset_ = localDataTableTag.getDataset();
			this.datasetName_ = localDataTableTag.getDataset().getId();
		}
		else if(this.datasetNameExpr_ != null)
		{
			this.datasetName_ = ((String) ExpressionEvaluatorManager.evaluate("dataset", this.datasetNameExpr_,
					String.class, this, this.pageContext));
			this.dataset_ = ((DataSet) this.pageContext.getAttribute(this.datasetName_));
		}
		else
		{
			throw new JspException(
					"Cannot find an appropriate dataset, dataset navigation tags must be inside a datatable tag or a dataset name must be specified");
		}
		int i = calculatePageNumber();
		this.enabled_ = ((i > 0) && (i <= this.dataset_.getAvailablePages()) && (i != this.dataset_.getCurrentPage()));
		if(this.enabled_)
			setProperty(this.datasetName_ + ".currentPage", String.valueOf(i));
		if(this.hideWhenDisabledExpr_ != null)
			this.hideWhenDisabled_ = ((Boolean) ExpressionEvaluatorManager.evaluate("hideWhenDisabled",
					this.hideWhenDisabledExpr_, Boolean.class, this, this.pageContext)).booleanValue();
		else
			this.hideWhenDisabled_ = false;
	}

	public int doStartTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		if(isEnabled())
			return super.doStartTag(paramAbstractComponentStateBean);
		if(this.hideWhenDisabled_)
			return 0;
		JspWriter localJspWriter = this.pageContext.getOut();
		try
		{
			HtmlPrintHelper.printHtmlStartTag("span", getHtmlClass(), getHtmlStyle(), localJspWriter);
		}
		catch (IOException localIOException)
		{
			logger_.error(localIOException.getMessage());
		}
		return 1;
	}

	public int doEndTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		if(isEnabled())
			return super.doEndTag(paramAbstractComponentStateBean);
		if(!(this.hideWhenDisabled_))
		{
			JspWriter localJspWriter = this.pageContext.getOut();
			try
			{
				HtmlPrintHelper.printHtmlEndTag("span", localJspWriter);
			}
			catch (IOException localIOException)
			{
				logger_.error(localIOException.getMessage());
			}
		}
		return 6;
	}

	protected abstract int calculatePageNumber();

	public void setHideWhenDisabled(String paramString)
	{
		this.hideWhenDisabledExpr_ = paramString;
	}

	public boolean isEnabled()
	{
		return this.enabled_;
	}

	public boolean isConditionTrue()
	{
		return this.enabled_;
	}

	public void setDataset(String paramString)
	{
		this.datasetNameExpr_ = paramString;
	}
}
